//
//  InformationViewModel.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/9/22.
//  Copyright © 2021 myTV. All rights reserved.
//

#import "InformationViewModel.h"
#import "InformationTypeListApi.h"
#import "InformationListApi.h"

@implementation InformationViewModel

- (InformationTypeListApi *)fetchInformationTypeListApi
{
    InformationTypeListApi *api = [[InformationTypeListApi alloc] init];
    return api;
}
- (InformationListApi *)fetchInformationBannerListApi
{
    InformationListApi *api = [[InformationListApi alloc] initWithType:@2];
    api.pageNum = 1;
    api.pageSize = 6;
    return api;
}

-(void)fetchInitData:(void(^)(void))block
{
    NSMutableArray *apiAry = [NSMutableArray array];
    [apiAry addObject:[self fetchInformationTypeListApi]];
    [apiAry addObject:[self fetchInformationBannerListApi]];
    
    YTKBatchRequest *batchRequest = [[YTKBatchRequest alloc] initWithRequestArray:apiAry];
    
    [batchRequest setCompletionBlockWithSuccess:^(YTKBatchRequest * _Nonnull batchRequest) {
        
        // 全部请求回来才会到这里
        [self handleTopBatchRequests:apiAry];
        block();

    } failure:^(YTKBatchRequest * _Nonnull batchRequest) {
        [self handleTopBatchRequests:apiAry];
        block();
    }];
    
    [batchRequest start];
    
    
}


- (void)handleTopBatchRequests:(NSArray *)requestArray {
    for (ZCRequest *request in requestArray) {

        if ([request isKindOfClass:[InformationTypeListApi class]]) {
//            InformationTypeListApi *api = (InformationTypeListApi *)request;
//            NSArray *types = [TypeModel mj_objectArrayWithKeyValuesArray:api.resArr];
            NSMutableArray *types = [[NSMutableArray alloc] init];
            TypeModel *model = [[TypeModel alloc] init];
            model.messageTypeId = @1;
            model.messageName = @"H5游戏";
            [types addObject:model];
            TypeModel *model1 = [[TypeModel alloc] init];
            model1.messageTypeId = @2;
            model1.messageName = @"下载过";
            [types addObject:model1];
            self.types = types;
        }
        if ([request isKindOfClass:[InformationListApi class]]) {
            InformationListApi *api = (InformationListApi *)request;
            NSMutableArray *dataArray = [InformationModel mj_objectArrayWithKeyValuesArray:api.resArr];
            self.modelList = dataArray;
        }
    }
}




@end
